﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ExpandScript : MonoBehaviour {

	ArrayList shapes = new ArrayList();
	bool expanding = false, contracting = false, collapse = false, expandingCancel = false, contractingCancel = false, firstStart = true;
	public bool expandedState = false;
	public Vector3 puzzleCenter = Vector3.zero;
	int children;
	Vector3 fallDirection = new Vector3(1, 0, 0);
	GridScript gridScript;

	// Use this for initialization
	void Start () {
		gridScript = gameObject.GetComponent<GridScript>();
		children = 0;/*transform.childCount;
		for (int i = 0; i < children; i++) {
			GameObject shape = transform.GetChild(i).gameObject;
			shapes.Add (shape);
			puzzleCenter += shape.transform.position;
		}
		puzzleCenter /= children;
		foreach (GameObject shape in shapes) {
			shape.GetComponent<ShapeScript>().setDestination(puzzleCenter);
		}*/
	}

	// Update is called once per frame
	void Update () {
		checkChildren ();
		if (Input.GetButtonUp("Fire1") && !contracting) {
			expanding = true;
			contracting = false;
		}else if (Input.GetButtonUp("Fire2") && !expanding) {
			contracting = true;
			expanding = false;
		}
		if (expanding) {
			expandingCancel = true;
			foreach (GameObject shape in shapes) {
				Vector3 destination = shape.GetComponent<ShapeScript>().getDestination();
				if(Mathf.Abs(Vector3.Distance(destination, shape.transform.position)) > 0.3f){
					expandingCancel = false;
				}
				shape.transform.position = Vector3.Lerp (shape.transform.position, destination, Time.deltaTime * 5f);
			}
			if (expandingCancel) {
				expanding = false;
				expandedState = true;
				expandingCancel = false;
			}
		}
		if (contracting) {
			//Debug.Log("CONTRACTING");
			contractingCancel = true;
			foreach (GameObject shape in shapes) {
				Vector3 destination = shape.GetComponent<ShapeScript>().getInitialPosition();
				shape.transform.position = Vector3.Lerp (shape.transform.position, destination, Time.deltaTime * 5f);
				//Debug.Log("CONTRACTINGMOVE");
				if(Mathf.Abs(Vector3.Distance(destination, shape.transform.position)) > 0.1f){
					contractingCancel = false;
				}
			}
			if (contractingCancel) {
				//Debug.Log("CONTRACTINGCANCEL");
				contractingCancel = false;
				contracting = false;
				expandedState = false;
			}
			if(!contracting){
				foreach (GameObject shape in shapes) {
					shape.transform.position = shape.GetComponent<ShapeScript>().getInitialPosition();
				}
			}
		}
	}

	public void setContracting(){
		contracting = true;
	}

	public GameObject getShapeById(int id){
		foreach (GameObject shape in shapes) {
			if(shape.GetComponent<ShapeScript>().getId()==id){
				return shape;
			}
		}
		return null;
	}

	public int getShapeIndexById(int id){
		int i = 0;
		foreach (GameObject shape in shapes) {
			if(shape.GetComponent<ShapeScript>().getId()==id){
				return i;
			}
			i++;
		}
		return -1;
	}

	public ArrayList getShapes(){
		return shapes;
	}

	public void checkChildren(){
		int newChildren = transform.childCount;
		if (!gridScript.isGridInitialized () && children != 0) {
			gridScript.updateGrid(shapes);
		}
		if (newChildren != children) {
			//Debug.Log ("ChildrenChange");
			collapse = true;
			puzzleCenter = Vector3.zero;
			children = newChildren;
			shapes.Clear ();
			for (int i = 0; i < children; i++) {
				GameObject shape = transform.GetChild (i).gameObject;
				if(firstStart){
					shape.GetComponent<ShapeScript>().setId(i);
				}
				shapes.Add (shape);
				puzzleCenter += shape.transform.position;
			}
			if(firstStart && children != 0){
				firstStart = false;
			}
			puzzleCenter /= children;
			foreach (GameObject shape in shapes) {
				shape.GetComponent<ShapeScript>().setDestination(puzzleCenter);
			}
			if (gridScript.isGridInitialized () && children != 0) {
				gridScript.doCollapseChanges();
				//gridScript.updateGrid(shapes);
			}
		}
	}
}
